package hander

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhibo/zhibo/api/router"
	"zhibo/zhibo/global"
	"zhibo/zhibo/internal/model"
)

func Create(c *gin.Context) {
	var req router.Create

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}
	video := model.Video{
		Name:     req.Name,
		Author:   req.Author,
		Duration: req.Duration,
	}
	if err := global.DB.Create(&video).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": video.ID,
	})
	return
}

func List(c *gin.Context) {
	var req router.List

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}
	var video []model.Video
	if err := global.DB.Find(&video).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "列表展示失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "列表展示失败",
		"data": video,
	})
	return
}

func Info(c *gin.Context) {
	var req router.Info

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}
	var video model.Video
	if err := global.DB.Where("id =?", req.Id).First(&video).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "详情展示失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "详情展示成功",
		"data": video,
	})
	return
}
